"""
The login frame is a simple dialog for user/pass entry
Written By: Daniel Ortiz
"""
import wx
class UserPassFrame(wx.Frame):
    """
    A frame which contains the textctrl to input the
    name and password
    """
    def __init__(self, parent, handler, name = 'Login'):
        """
        The constructor of the UserPass Frame
        """
        size = (200, 100)
        dis = size[0]/15
        self.name = name
        self.parent = parent
        self.handler = handler
        wx.Frame.__init__(self, parent, -1, name, size = size)
        panel = wx.Panel(self, -1)
        self.CenterOnParent()

        # User input box
        userLabel = wx.StaticText(panel, -1, 'User:')
        self.userText = wx.TextCtrl(panel, -1, '', size = (size[0] / 2, -1))
        self.userText.SetInsertionPoint(0)

        # Password input box
        passLabel = wx.StaticText(panel, -1, 'Password:')
        self.passText = wx.TextCtrl(panel, -1, '', size = (size[0]/2, -1),
                                    style = wx.TE_PASSWORD)

        # Buttons
        self.cancelButton = wx.Button(panel, label = 'Cancel', size = (size[0]/2 - dis, -1))
        self.Bind(wx.EVT_BUTTON, self.OnCancel, self.cancelButton)
        self.okButton = wx.Button(panel, label = 'Ok', size = (size[0]/2 - dis, -1))
        self.Bind(wx.EVT_BUTTON, self.OnOk, self.okButton)

        # Add a sizer
        self.sizer = wx.FlexGridSizer(cols = 2, hgap = 6, vgap = 6)
        self.sizer.AddMany([userLabel, self.userText, passLabel, self.passText,
                       self.cancelButton, self.okButton])
        panel.SetSizer(self.sizer)


    def OnCancel(self, event):
        """
        If the user cancels, just exit
        """
        self.Close()

    def OnOk(self, event):
        """
        Validate the password/user pair. If valid, set the parent frame state variables.
        If not, render the window telling the user that the login failed.
        """
        self.password = self.passText.GetValue()
        self.username = self.userText.GetValue()
        self.handler.ValidateUser(self.username, self.password)